package cn.darkhorse.mesadmin.controller.workflow;


import cn.darkhorse.common.constant.CommonConstant;
import cn.darkhorse.workflow.auto.entity.BpmnSysUser;
import cn.darkhorse.workflow.auto.entity.ProjectApprovalOpinions;
import cn.darkhorse.workflow.service.IApprovalService;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: mr.wang
 * @time: 2020/7/6 0006 14:14
 */
@RestController
@RequestMapping("/approval")
public class ApprovalController extends BaseController {
	
	@Autowired
	IApprovalService iApprovalService;
	@Autowired
	ISysUserService sysUserService;
	
	/**
	 * 选取可用工作流
	 * @param typeId
	 * @return
	 */
	@RequestMapping(value = "/chooseworkflow",method = RequestMethod.POST)
	public AjaxResult chooseWorkflow(String typeId){
		return AjaxResult.success(iApprovalService.chooseWorkflow(typeId, SecurityUtils.getUsername()));
	}
	
	
	/**
	 * 待办事项（临时）
	 * @param userName
	 */
	@RequestMapping(value = "/schedule",method = RequestMethod.POST)
	public AjaxResult selectSchedule(String userName){
		if(StringUtils.isBlank(userName)){
			return AjaxResult.error("缺失参数值!");
		}
		SysUser user = sysUserService.selectUserByUserName(userName);
		if(user == null || UserConstants.USER_DISABLE.equals(user.getStatus())){
			return AjaxResult.success();
		}
		List<Map<String,Object>> mapList = iApprovalService.selectSchedule(userName);
		return AjaxResult.success(mapList);
	}
	
	
	/**
	 * 用户交接人查看代办事项
	 * @param userName
	 * @return
	 */
	/*@RequestMapping(value = "/schedule2",method = RequestMethod.POST)
	public AjaxResult selectSchedule2(String userName){
		if(StringUtils.isBlank(userName)){
			return AjaxResult.error("缺失参数值!");
		}
		SysUser user = sysUserService.selectUserByUserName(userName);
		if(StringUtils.isNotBlank(user.getHandoverPerson())){
			return AjaxResult.success();
		}
		List<Map<String,Object>> mapList = iApprovalService.selectSchedule(userName);
		return AjaxResult.success(mapList);
	}*/
	
	
	
	/**
	 * 获取审批表意见
	 * @param businessId
	 * @return
	 */
	@RequestMapping(value = "/approvalinfo",method = RequestMethod.POST)
	public AjaxResult getApprovalFormInfo(String businessId,String businessType){
		if(StringUtils.isBlank(businessId)){
			return AjaxResult.error("缺失参数值!");
		}
		List<ProjectApprovalOpinions> mapList = iApprovalService.getApprovalFormInfo(businessId,businessType);
		return AjaxResult.success(mapList);
	}
	
	/**
	 * 获取审批按钮权限(临时)
	 * @param taskKey
	 */
	@RequestMapping(value = "/approvalauthority",method = RequestMethod.POST)
	public AjaxResult getApprovalAuthority(String taskKey,String taskId,String workflowExampleId){
		if(StringUtils.isBlank(taskKey) || StringUtils.isBlank(taskId) || StringUtils.isBlank(workflowExampleId)){
			return AjaxResult.error("缺失参数值!");
		}
		Map<String,Object> mapList = iApprovalService.getApprovalAuthority(taskKey,taskId,workflowExampleId);
		return AjaxResult.success(mapList);
	}
	
	
	/**
	 * 获取单据审批节点流程图
	 * @param businessId
	 * @param businessType
	 * @return
	 */
	@RequestMapping(value = "/flow-chart",method = RequestMethod.POST)
	public AjaxResult getFlowChart(Long businessId,String businessType,String workflowExampleId){
		if(businessId == null || StringUtils.isBlank(businessType)){
			return AjaxResult.error("缺失参数值!");
		}
		Map<String, Object> flowChart = iApprovalService.getFlowChart(businessId, businessType,workflowExampleId);
		return AjaxResult.success(flowChart);
	}
	
	/**
	 * 获取下一办理人
	 * @param businessId
	 * @param businessType
	 * @return
	 */
	@RequestMapping(value = "/next-node",method = RequestMethod.POST)
	public AjaxResult getNextNode(String workFlowId, String businessId, String businessType){
		if(businessId == null || StringUtils.isBlank(businessType)){
			return AjaxResult.error("缺失参数值!");
		}
		JSONArray nextNode = iApprovalService.getNextNode(workFlowId, businessId, businessType,getStopUsers());
		return AjaxResult.success(nextNode);
	}
	
	/**
	 * 用户集合
	 * @return
	 */
	private List<BpmnSysUser> getStopUsers(){
		List<BpmnSysUser> bpmnSysUsers = new ArrayList<>();
		SysUser user = new SysUser();
		user.setDelFlag(String.valueOf(CommonConstant.IS_DELETE_NO));
		List<SysUser> sysUsers = sysUserService.selectUserList(user);
		sysUsers.forEach(item->{
			BpmnSysUser bpmnSysUser = new BpmnSysUser();
			BeanUtil.copyProperties(item,bpmnSysUser);
			bpmnSysUsers.add(bpmnSysUser);
		});
		return bpmnSysUsers;
	}
	
	
	
	
	
	
}
